package ysq.xyg.shop.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.shop.dao.ShopWithdrawDao;
import ysq.xyg.shop.model.po.ShopWithdraw;
import ysq.xyg.shop.model.vo.ShopWithdrawVo;
import ysq.xyg.shop.service.ShopWithdrawService;
import ysq.xyg.sys.model.po.SysUser;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 商城模块-提现表service实现
 *
 * @author congcong tao
 * @date 2019-4-28 11:19:58
 */
@Service("shopWithdrawService")
public class ShopWithdrawServiceImpl extends BaseServiceImpl<ShopWithdraw> implements ShopWithdrawService {

    @Resource
    private ShopWithdrawDao shopWithdrawDao;

    private ShopWithdraw shopWithdraw;

    @Autowired
    public void setBaseDao(ShopWithdrawDao shopWithdrawDao) {
        this.baseDao = shopWithdrawDao;
    }

    /**
     * 分页查询
     *
     * @param shopWithdrawVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-4-28 11:19:58
     */
    @Override
    public ReturnEntity page(ShopWithdrawVo shopWithdrawVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<ShopWithdraw> list = acceptList(shopWithdrawVo);
        PageInfo<ShopWithdraw> page=new PageInfo<ShopWithdraw>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    @Override
    public ReturnEntity pageLeft(ShopWithdrawVo shopWithdrawVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<ShopWithdraw> list = shopWithdrawDao.selectEntityList(shopWithdrawVo);
        PageInfo<ShopWithdraw> page=new PageInfo<ShopWithdraw>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param shopWithdrawVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-4-28 11:19:58
     */
    @Override
    public ReturnEntity addOrUpdate(ShopWithdrawVo shopWithdrawVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(shopWithdrawVo.getId())){
            shopWithdraw=new ShopWithdraw(currentUser);
            isUpate=false;
        }else{
            shopWithdraw=selectByPrimaryKey(shopWithdrawVo.getId());
            if(ObjectUtil.isNull(shopWithdraw)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            shopWithdraw.setUpdateTime(new Date());
            shopWithdraw.setUpdateUser(currentUser.getId());
        }
        shopWithdraw.setMemberId(shopWithdrawVo.getMemberId());
        shopWithdraw.setMoney(shopWithdrawVo.getMoney());
        shopWithdraw.setAlipay(shopWithdrawVo.getAlipay());
        shopWithdraw.setBankCard(shopWithdrawVo.getBankCard());
        shopWithdraw.setStatus(shopWithdrawVo.getStatus());
        shopWithdraw.setManageStatus(shopWithdrawVo.getManageStatus());
        shopWithdraw.setDescription(shopWithdrawVo.getDescription());
        shopWithdraw.setApplyTime(shopWithdrawVo.getApplyTime());
        shopWithdraw.setManageTime(shopWithdrawVo.getManageTime());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(shopWithdraw);
        }else{
            num=insert(shopWithdraw);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-4-28 11:19:58
     */
    @Override
    public ReturnEntity delete(String id, SysUser currentUser) {
        ShopWithdraw shopWithdraw=selectByPrimaryKey(id);
        if(shopWithdraw!=null){
            shopWithdraw.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            shopWithdraw.setUpdateTime(new Date());
            shopWithdraw.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(shopWithdraw);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param shopWithdrawVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2019-4-28 11:19:58
     */
    @Override
    public List<ShopWithdraw> acceptList(ShopWithdrawVo shopWithdrawVo){
        Example example = new Example(ShopWithdraw.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(shopWithdrawVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",shopWithdrawVo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return shopWithdrawDao.selectByExample(example);
    }

    @Override
    public List<ShopWithdraw> selectEntityList(ShopWithdrawVo shopWithdrawVo) {
        return shopWithdrawDao.selectEntityList(shopWithdrawVo);
    }
}
